<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:isc="http://extension-functions.intersystems.com" xmlns:hl7="urn:hl7-org:v3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:exsl="http://exslt.org/common" exclude-result-prefixes="isc hl7 xsi exsl">

		
	<!-- 既往史章节 -->
	<xsl:template match="*" mode="HistoryOfPastIllnessSection">
		<xsl:apply-templates select="$sectionRootPath[hl7:code/@code=$hcc_HistoryOfPastIllness]" mode="HistoryOfPastIllnessEntry">
		</xsl:apply-templates>
	</xsl:template>
	<!-- 既往史章节条目 -->
	<xsl:template match="*" mode="HistoryOfPastIllnessEntry">
		<xsl:apply-templates select="hl7:entry/hl7:observation[hl7:code/@code='DE02.10.099.00' or hl7:code/@code='DE02.10.061.00' or hl7:code/@code='DE02.10.098.00' or hl7:code/@code='DE02.10.026.00' or hl7:code/@code='DE02.10.008.00' or hl7:code/@code='DE02.10.101.00' or hl7:code/@code='DE02.10.100.00' or hl7:code/@code='DE05.10.031.00' or hl7:code/@code='DE05.10.119.00']" mode="HCCProblem">
			<xsl:with-param name="sectionType" select="'HPA'"/>
		</xsl:apply-templates>
	</xsl:template>	

	<!-- 现病史章节 -->
	<xsl:template match="*" mode="HistoryOfPresentIllnessSection">
		<xsl:apply-templates select="$sectionRootPath[hl7:code/@code=$hcc_HistoryOfPresentIllness]" mode="HistoryOfPresentIllnessEntry">
		</xsl:apply-templates>
	</xsl:template>
	<!-- 现病史章节条目 -->
	<xsl:template match="*" mode="HistoryOfPresentIllnessEntry">
		<xsl:apply-templates select="hl7:entry/hl7:observation" mode="HCCProblem">
			<xsl:with-param name="sectionType" select="'HPR'"/>
		</xsl:apply-templates>
	</xsl:template>	
	
	<!-- 症状章节 & 主要健康问题章节 -->
	<xsl:template match="*" mode="ProblemListSection">
		<xsl:choose>
		    <!-- C0011，C0012，C0032，C0033，C0013，C0024，C0034不处理 -->
			<xsl:when test="$hccDocType='C0006' or $hccDocType='C0023' or $hccDocType='C0014' or $hccDocType='C0035' or $hccDocType='C0036' or $hccDocType='C0038' or $hccDocType='C0049' or $hccDocType='C0053'">
				<xsl:apply-templates select="$sectionRootPath[hl7:code/@code=$hcc_ProblemList]" mode="ProblemListEntry">
			</xsl:apply-templates>			
		</xsl:when>
		</xsl:choose>
	</xsl:template>
	<!-- 症状章节&主要健康问题章节条目 -->
	<xsl:template match="*" mode="ProblemListEntry">
		<xsl:apply-templates select="hl7:entry/hl7:observation[hl7:code/@code='DE04.01.117.00' or hl7:code/@code='DE04.01.118.00' or hl7:code/@code='DE05.10.070.00' or hl7:code/@code='DE05.10.148.00' or hl7:code/@code='DE05.10.161.00' or hl7:code/@code='DE02.10.028.00' or hl7:code/@code='DE06.00.309.00']" mode="HCCProblem">
			<xsl:with-param name="sectionType" select="'PL'"/>
		</xsl:apply-templates>
	</xsl:template>
	
	<!--通用Problem条目输出模板-->
	<xsl:template match="*" mode="HCCProblem">
		<xsl:param name = "sectionType" />
		<Problem>
		<EncounterNumber><xsl:value-of select="$encompassingEncounterID"/></EncounterNumber>
		<ExternalId><xsl:value-of select="concat($hccDocNo,'-',$sectionType,'-',position())"/></ExternalId>			
		<xsl:choose>
		    <!-- 既往史章节 -->
			<xsl:when test="$sectionType='HPA'">
			    <xsl:choose>
					<!-- 《入院记录》 -->
					<xsl:when test="$hccDocType='C0034'">
						<xsl:variable name="obsType" select="hl7:code/@code"/>
						<xsl:choose>
							<!-- 一般健康状况标志 & 患者传染性标志 -->
							<xsl:when test="$obsType='DE05.10.031.00' or $obsType='DE05.10.119.00'">
								<Category>
									<Code><xsl:value-of select="hl7:entryRelationship/hl7:observation/hl7:code/@code"/></Code>
									<Description><xsl:value-of select="hl7:entryRelationship/hl7:observation/hl7:code/@displayName"/></Description>
								</Category>
								<ProblemDetails>
									<xsl:value-of select="hl7:entryRelationship/hl7:observation/hl7:value"/>
								</ProblemDetails>
							</xsl:when>
							<xsl:otherwise>
								<Category>
									<Code><xsl:value-of select="hl7:code/@code"/></Code>
									<Description><xsl:value-of select="hl7:code/@displayName"/></Description>
								</Category>
								<ProblemDetails>
									<xsl:value-of select="hl7:value"/>
								</ProblemDetails>
							</xsl:otherwise>
						</xsl:choose>
					</xsl:when>
					<!-- 非《入院记录》 -->
					<xsl:otherwise>
						<Category>
							<Code><xsl:value-of select="hl7:code/@code"/></Code>
							<Description><xsl:value-of select="hl7:code/@displayName"/></Description>
						</Category>
						<ProblemDetails>
							<xsl:value-of select="hl7:value"/>
						</ProblemDetails>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:when>
			<!-- 现病史章节 -->
			<xsl:when test="$sectionType='HPR'">
				<Category>
					<Code><xsl:value-of select="hl7:code/@code"/></Code>
					<Description><xsl:value-of select="hl7:code/@displayName"/></Description>
				</Category>
				<ProblemDetails>
					<xsl:value-of select="hl7:value"/>
				</ProblemDetails>
			</xsl:when>
			<!-- 症状章节 & 主要健康问题章节 -->
			<xsl:when test="$sectionType='PL'">
				<Category>
					<Code><xsl:value-of select="hl7:code/@code"/></Code>
					<Description><xsl:value-of select="hl7:code/@displayName"/></Description>
				</Category>
				<ProblemDetails>
					<xsl:value-of select="hl7:value"/>
				</ProblemDetails>
				<!--<xsl:if test="string-length(hl7:effectiveTime/low/@value)">
					<FromTime><xsl:value-of select="hl7:effectiveTime/low/@value"/></FromTime>
					<ToTime><xsl:value-of select="hl7:effectiveTime/high/@value"/></ToTime>
				</xsl:if>-->
			</xsl:when>
		</xsl:choose>
		</Problem>		
	</xsl:template>
	
</xsl:stylesheet>
